//
//  ElevatorViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-12-14.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "ElevatorViewController.h"

@interface ElevatorViewController ()
{
    UIImage *roundSelect;
    UIImage *roundUnselect;
    NSMutableArray *checkIds;
    NSMutableArray *isNormals;
    NSArray *originCheckIds;
}
@end

@implementation ElevatorViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"垂直电梯自检记录";
    [super viewDidLoad];
    NSArray *queryDataArr = @[@{@"floorId": @"1",@"floorname":@"1F",@"rfid":@"<#string#>"},@{@"floorId": @"2",@"floorname":@"2F",@"rfid":@"<#string#>"},@{@"floorId": @"3",@"floorname":@"3F",@"rfid":@"<#string#>"},@{@"floorId": @"4",@"floorname":@"4F",@"rfid":@"<#string#>"},@{@"floorId": @"5",@"floorname":@"5F",@"rfid":@"<#string#>"},@{@"floorId": @"6",@"floorname":@"6F",@"rfid":@"<#string#>"},@{@"floorId": @"7",@"floorname":@"7F",@"rfid":@"<#string#>"},@{@"floorId": @"8",@"floorname":@"8F",@"rfid":@"<#string#>"},@{@"floorId": @"9",@"floorname":@"9F",@"rfid":@"<#string#>"},@{@"floorId": @"10",@"floorname":@"10F",@"rfid":@"<#string#>"},@{@"floorId": @"11",@"floorname":@"11F",@"rfid":@"<#string#>"},@{@"floorId": @"12",@"floorname":@"12F",@"rfid":@"<#string#>"},@{@"floorId": @"13",@"floorname":@"13F",@"rfid":@"<#string#>"},@{@"floorId": @"14",@"floorname":@"14F",@"rfid":@"<#string#>"},@{@"floorId": @"15",@"floorname":@"15F",@"rfid":@"<#string#>"},@{@"floorId": @"16",@"floorname":@"16F",@"rfid":@"<#string#>"},@{@"floorId": @"17",@"floorname":@"17F",@"rfid":@"<#string#>"},@{@"floorId": @"18",@"floorname":@"18F",@"rfid":@"<#string#>"},@{@"floorId": @"19",@"floorname":@"19F",@"rfid":@"<#string#>"},@{@"floorId": @"20",@"floorname":@"20F",@"rfid":@"<#string#>"},@{@"floorId": @"21",@"floorname":@"21F",@"rfid":@"<#string#>"},@{@"floorId": @"22",@"floorname":@"22F",@"rfid":@"<#string#>"},@{@"floorId": @"23",@"floorname":@"23F",@"rfid":@"<#string#>"},@{@"floorId": @"24",@"floorname":@"24F",@"rfid":@"<#string#>"},@{@"floorId": @"25",@"floorname":@"25F",@"rfid":@"<#string#>"},@{@"floorId": @"26",@"floorname":@"26F",@"rfid":@"<#string#>"},@{@"floorId": @"27",@"floorname":@"27F",@"rfid":@"<#string#>"},@{@"floorId": @"28",@"floorname":@"28F",@"rfid":@"<#string#>"},@{@"floorId": @"29",@"floorname":@"29F",@"rfid":@"<#string#>"},@{@"floorId": @"30",@"floorname":@"30F",@"rfid":@"<#string#>"},@{@"floorId": @"31",@"floorname":@"31F",@"rfid":@"<#string#>"},@{@"floorId": @"32",@"floorname":@"32F",@"rfid":@"<#string#>"},@{@"floorId": @"33",@"floorname":@"33F",@"rfid":@"<#string#>"},@{@"floorId": @"34",@"floorname":@"34F",@"rfid":@"<#string#>"},@{@"floorId": @"35",@"floorname":@"35F",@"rfid":@"<#string#>"},@{@"floorId": @"36",@"floorname":@"36F",@"rfid":@"<#string#>"},@{@"floorId": @"37",@"floorname":@"37F",@"rfid":@"<#string#>"}];
    self.floorNames = [NSMutableArray array];
    self.floorIds = [NSMutableArray array];
    originCheckIds = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
    for (NSDictionary *dic in queryDataArr)
    {
        [self.floorNames addObject:[dic valueForKey:@"floorname"]];
        [self.floorIds addObject:[dic valueForKey:@"floorId"]];
        [self.sendFloorDatas addObject:@""];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[dic valueForKey:@"floorId"] }];
    }
    self.sendUrl = addElevator;
    [self initData];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle];

    [self showUIWithArray:self.allShowDataArr andFloorId:@"1"];
}

- (void)initData
{
    roundSelect = [UIImage imageNamed:@"选中"];
    roundUnselect = [UIImage imageNamed:@"未选中"];
    NSDictionary *threeDic = @{@"checkId": @"1",@"checkName":@"3#"};
    NSDictionary *fourDic = @{@"checkId": @"2",@"checkName":@"4#"};
    NSDictionary *fiveDic = @{@"checkId": @"3",@"checkName":@"5#"};
    NSDictionary *sixDic = @{@"checkId": @"4",@"checkName":@"6#"};
    NSDictionary *sevenDic = @{@"checkId": @"5",@"checkName":@"7#"};
    NSDictionary *eightDic = @{@"checkId": @"6",@"checkName":@"8#"};
    NSDictionary *nineDic = @{@"checkId": @"7",@"checkName":@"9#"};
    NSDictionary *tenDic = @{@"checkId": @"8",@"checkName":@"10#"};
    NSDictionary *elevenDic = @{@"checkId": @"9",@"checkName":@"11#"};
    NSDictionary *twelveDic = @{@"checkId": @"10",@"checkName":@"12#"};
    self.allShowDataArr = @[threeDic,fourDic,fiveDic,sixDic,sevenDic,eightDic,nineDic,tenDic,elevenDic,twelveDic];
}
- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    [self clearAll];
    isNormals = [NSMutableArray array];
    checkIds = [NSMutableArray array];
    self.floorIdQuanju = floorId;
    id receiveFloorObj = [self.sendFloorDatas objectAtIndex:[self.floorIds indexOfObjectss:self.floorIdQuanju]];
    if (![receiveFloorObj isKindOfClass:[NSString class]])
    {
        self.sendCheckDatas = [receiveFloorObj objectForKey:@"checkData"];
    }
    else
    {
        self.sendCheckDatas = [NSMutableArray array];
        for (int i = 0; i < array.count; i ++)
        {
            if ([floorId integerValue] > 8 && i < 3)
            {
                NSDictionary *sendDic = @{@"checkId": [NSString stringWithFormat:@"%d",i+1],@"result":@"3"};
                [self.sendCheckDatas addObject:sendDic];
                [checkIds addObject:[NSString stringWithFormat:@"%d",i+1]];
            }
            else
            {
                NSDictionary *sendDic = @{@"checkId": [NSString stringWithFormat:@"%d",i+1],@"result":@"3"};
                [self.sendCheckDatas addObject:sendDic];
                [checkIds addObject:[NSString stringWithFormat:@"%d",i+1]];
            }
        }
    }
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    if ([floorId integerValue] >=1 && [floorId integerValue] <= 5)
    {
        [indexSet addIndexesInRange:NSMakeRange(0, 10)];
    }
    else if ([floorId integerValue] >= 6 && [floorId integerValue] <= 8)
    {
        [indexSet addIndexesInRange:NSMakeRange(1, 6)];
    }
    else if ([floorId integerValue] >= 9 && [floorId integerValue] <= 21)
    {
        [indexSet addIndexesInRange:NSMakeRange(0, 3)];
    }
    else if ([floorId integerValue] >= 22 && [floorId integerValue] <= 31)
    {
        [indexSet addIndex:0];
    }
    else if ([floorId integerValue] >= 32 && [floorId integerValue] <= 37)
    {
        [indexSet addIndexesInRange:NSMakeRange(1, 2)];
    }
    NSArray *dataArr = [array objectsAtIndexes:indexSet];
    for (int i = 0; i < dataArr.count; i++)
    {
        NSDictionary *dataDic = [dataArr objectAtIndex:i];
        NSString *checkId = [dataDic valueForKey:@"checkId"];
        [checkIds addObject:checkId];
        NSString *checkName = [dataDic valueForKey:@"checkName"];
        UILabel *checkNameLb = [UILabel new];
        checkNameLb.text = checkName;
        checkNameLb.font = [UIFont systemFontOfSize:25];
        CGSize mysz = [checkNameLb.text sizeWithFont:checkNameLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        checkNameLb.frame = CGRectMake(10, [[self.controlWillClears lastObject]frame].origin.y + [[self.controlWillClears lastObject]frame].size.height + interval111, mysz.width, mysz.height);
        [self.backScrollV addSubview:checkNameLb];
        [self.checkNameLbs addObject:checkNameLb];
        [self.controlWillClears addObject:checkNameLb];
        [self addSingleSelect:@[@"正常",@"不正常"] andResult:[[self.sendCheckDatas objectAtIndex:[originCheckIds indexOfObject:checkId]]valueForKey:@"result"]];
    }
    [self addCommentKuang:@""];
}
- (void)addSingleSelect:(NSArray *)options andResult:(NSString *)receiveString
{
    NSMutableArray *btnArr = [NSMutableArray array];
    for (int j =0; j<options.count; j++)
    {
        UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [selectBtn setImage:roundUnselect forState:UIControlStateNormal];
                                            if ([receiveString isEqualToString:@"3"]) {
            [selectBtn setImage:roundUnselect forState:UIControlStateNormal];
        }
        else if ([receiveString isEqualToString:[NSString stringWithFormat:@"%d",j]])
        {
            [selectBtn setImage:roundSelect forState:UIControlStateNormal];
        }
        [selectBtn addTarget:self action:@selector(selectOptions:) forControlEvents:UIControlEventTouchUpInside];
        selectBtn.frame = CGRectMake(62 + (self.backScrollV.frame.size.width - 62)/2*j, [[self.checkNameLbs lastObject]frame].origin.y + ([[self.checkNameLbs lastObject]frame].size.height + 10), roundSelect.size.width, roundSelect.size.height);
        [self.backScrollV addSubview:selectBtn];
        [self.controlWillClears addObject:selectBtn];
        [btnArr addObject:selectBtn];
        
        UILabel *abnormLb = [UILabel new];
        NSString *abnormStr = [options objectAtIndex:j];
        abnormLb.font = [UIFont systemFontOfSize:26];
        abnormLb.text = abnormStr;
        CGSize mySize = [abnormLb.text sizeWithFont:abnormLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        abnormLb.frame = CGRectMake(selectBtn.frame.origin.x + selectBtn.frame.size.width, selectBtn.frame.origin.y, mySize.width, mySize.height);
        [self.backScrollV addSubview:abnormLb];
        [self.controlWillClears addObject:abnormLb];
        [isNormals addObject:@""];
    }
    [self.selectBtnArr addObject:btnArr];
    [self.seleBtnData addObject:@""];
}
- (void)selectOptions:(id)sender
{
    UIButton *btn = (UIButton*)sender;
    for (int a = 0;a<self.selectBtnArr.count;a++)
    {
        NSArray *btnArr = [self.selectBtnArr objectAtIndex:a];
        if ([btnArr containsObject:btn])
        {
            for (int j = 0;j<btnArr.count;j++)
            {
                UIButton *seleBtn = [btnArr objectAtIndex:j];
                [seleBtn setImage:roundUnselect forState:UIControlStateNormal];
                if (btn == [btnArr objectAtIndex:j])
                {
                    [seleBtn setImage:roundSelect forState:UIControlStateNormal];
                    [isNormals replaceObjectAtIndex:a withObject:[NSString stringWithFormat:@"%d",j]];
                    if (j == 0)
                    {
                        [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
                    }
                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[checkIds objectAtIndex:a],@"checkId",[NSString stringWithFormat:@"%d",j],@"result",nil];
                    NSString *checkId = [checkIds objectAtIndex:a];
                    [self.sendCheckDatas replaceObjectAtIndex:[originCheckIds indexOfObject:checkId] withObject:dic];
                }
            }
        }
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTableView) {
        return self.floorNames.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (tableView == self.myTableView) {
        self.mycell.textLabel.text = [self.floorNames objectAtIndex:indexPath.row];
        return self.mycell;
    }
    return nil;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self hiddenAll];
    if (tableView == self.myTableView)
    {
        [self showUIWithArray:self.allShowDataArr andFloorId:[self.floorIds objectAtIndex:indexPath.row]];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
